package com.whnk.pm.service.impl;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.whnk.pm.domain.ImpSignInfo;
import com.whnk.pm.domain.OutsourceInfo;
import com.whnk.pm.dto.OutsourceExcelDTO;
import com.whnk.pm.dto.SignExcelDTO;
import com.whnk.pm.mapper.OutsourceInfoMapper;
import com.whnk.pm.service.IOutsourceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.common.support.Convert;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 外包人员基本 服务层实现
 *
 * @author whwhnk
 * @date 2020-01-08
 */
@Service
@Slf4j
public class OutsourceInfoServiceImpl implements IOutsourceInfoService {
    @Autowired
    private OutsourceInfoMapper outsourceInfoMapper;

    /**
     * 查询外包人员基本信息
     *
     * @param id 外包人员基本ID
     * @return 外包人员基本信息
     */
    @Override
    public OutsourceInfo selectOutsourceInfoById(Integer id) {
        return outsourceInfoMapper.selectOutsourceInfoById(id);
    }

    /**
     * 查询外包人员基本列表
     *
     * @param outsourceInfo 外包人员基本信息
     * @return 外包人员基本集合
     */
    @Override
    public List<OutsourceInfo> selectOutsourceInfoList(OutsourceInfo outsourceInfo) {
        return outsourceInfoMapper.selectOutsourceInfoList(outsourceInfo);
    }

    /**
     * 新增外包人员基本
     *
     * @param outsourceInfo 外包人员基本信息
     * @return 结果
     */
    @Override
    public int insertOutsourceInfo(OutsourceInfo outsourceInfo) {
        return outsourceInfoMapper.insertOutsourceInfo(outsourceInfo);
    }

    /**
     * 修改外包人员基本
     *
     * @param outsourceInfo 外包人员基本信息
     * @return 结果
     */
    @Override
    public int updateOutsourceInfo(OutsourceInfo outsourceInfo) {
        return outsourceInfoMapper.updateOutsourceInfo(outsourceInfo);
    }

    /**
     * 删除外包人员基本对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOutsourceInfoByIds(String ids) {
        return outsourceInfoMapper.deleteOutsourceInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 外包人员信息导入
     *
     * @param file
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void outsourceExcelFileUpload(MultipartFile file) throws IOException {
        try (ExcelReader reader = ExcelUtil.getReader(file.getInputStream())) {
            List<Map<String, Object>> mapList = reader.read(0, 1, Integer.MAX_VALUE);
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyyMMdd").create();
            List<OutsourceExcelDTO> outsourceExcelDTOList = mapList.stream()
                    .map(k -> gson.fromJson(gson.toJson(k), OutsourceExcelDTO.class)).collect(Collectors.toList());
            List<OutsourceInfo> outsourceInfoArrayList =
                    outsourceExcelDTOList.stream()/*.filter(k -> (k.getApproachTime() != null && k.getExitTime() == null))*/
                            .map(outsource -> {
                                OutsourceInfo outsourceInfo = new OutsourceInfo();
                                BeanUtils.copyProperties(outsource, outsourceInfo);
                                return outsourceInfo;
                            }).collect(Collectors.toList());

            outsourceInfoArrayList.sort(Comparator.comparing((k1) -> (
                    (k1.getApproachTime() == null ? new Date(0): k1.getApproachTime())
            )));

            outsourceInfoArrayList.forEach(k -> {
                if (StringUtils.equals(k.getQualification(), "工程师")) {
                    k.setQualification("中级工程师");
                }
            });

            for (OutsourceInfo outsourceInfo : outsourceInfoArrayList) {
                OutsourceInfo selectOutsource = new OutsourceInfo();
                selectOutsource.setIdCardNo(outsourceInfo.getIdCardNo());
                selectOutsource.setItemNumber(outsourceInfo.getItemNumber());
                List<OutsourceInfo> outsourceInfoResult = outsourceInfoMapper.selectOutsourceInfoListByOutsourceInfo(selectOutsource);
                if (outsourceInfoResult != null) {
                    outsourceInfoResult.forEach(k -> outsourceInfoMapper.deleteOutsourceInfoById(k.getId()));
                }
                outsourceInfoMapper.insertOutsourceInfo(outsourceInfo);
            }
        }
    }
}
